//
//  MYGSystemMessageHeaderView.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/2/29.
//

import UIKit

enum MYGSystemMessageType: Int {
    case system = 5
    case social = 6
    case order = 1
    case schedule = 7
    
    var image: String {
        switch self {
        case .system:
            return "system"
        case .social:
            return "social"
        case .order:
            return "order"
        case .schedule:
            return "schedule"
        }
    }
    
    var typeStr: String {
        switch self {
        case .system:
            return "5"
        case .social:
            return "6"
        case .order:
            return "1"
        case .schedule:
            return "7"
        }
    }
    
    var name: String {
        switch self {
        case .system:
            return "系统消息"
        case .social:
            return "分诊待办"
        case .order:
            return "好友申请"
        case .schedule:
            return "协会通知"
        }
    }
}

class MYGSystemMessageHeaderView: UIView {
    // MARK: - Property
    var contents: [MYGSystemTypeCellModel] = []
    
    var typeChange: ((MYGSystemMessageType)->Void)?
    
    var isShowSchedule:Bool = true {
        didSet {
            configItem()
        }
    }
    private var lastSel: Int?
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.lastSel = 0
        configUI()
       
        getMessageNum()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 配置Cell
    private func configItem() {
        let systemItem = MYGSystemTypeCellModel(image: "system", name: "系统消息", isSel: false,num: 0,type: .system)
        let socialItem = MYGSystemTypeCellModel(image: "social", name: "分诊待办", isSel: false,num: 0,type: .social)
        let orderItem = MYGSystemTypeCellModel(image: "order", name: "好友申请", isSel: false,num: 0,type: .order)
        let scheduleItem = MYGSystemTypeCellModel(image: "schedule", name: "协会通知", isSel: false,num: 0,type: .schedule)
        if isShowSchedule {
            contents = [systemItem, socialItem, orderItem, scheduleItem]
        }else {
            contents = [systemItem, socialItem, orderItem]
        }
        
        collectionView.reloadData()
    }
    
    // MARK: - UI
    private func configUI() {
        addSubview(collectionView)
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    // MARK: - lazy load
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: 72, height: 85)
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = (screenWidth-72*4-30)/3
        layout.sectionInset = .init(top: 16, left: 15, bottom: 0, right: 15)
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.dataSource = self
        cv.delegate = self
        cv.register(cellWithClass: MYGSystemMessageTypeCell.self)
        return cv
    }()
}

extension MYGSystemMessageHeaderView: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        contents.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withClass: MYGSystemMessageTypeCell.self, for: indexPath)
        cell.model = contents[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let lastSel = lastSel {
            if lastSel == indexPath.row {
                return
            }
            
            contents[lastSel].isSel = false
        }
        
        contents[indexPath.row].isSel = true
        lastSel = indexPath.row
        collectionView.reloadData()
        let type = contents[indexPath.row].type
        typeChange?(type)
    }
}

extension MYGSystemMessageHeaderView {
    
    func getMessageNum() {
        
        MMYGNetwork<MYGMessageApi, MYGMessageNumModel>.request(target: .getMessageNum) { [self] item in
            
            contents.removeAll()
            var str = 0
            var norStr = 0
            var time = 0
            var order = 0
            for model  in item!.data {
                if model.type == 1 {
                    order = model.count
                }else if (model.type == 5){
                    norStr = model.count
                }else if (model.type == 7){
                    time = model.count
                }else {
                    str = model.count
                }
                    
            }
            //0普通消息 1申请添加好友 2临床实验 3日程 4论文 5系统消息 6分享患者 7协会 8随身影像 9 社区患者
            let systemItem = MYGSystemTypeCellModel(image: "system", name: "系统消息", isSel: false,num: norStr,type: .system)
            let socialItem = MYGSystemTypeCellModel(image: "social", name: "分诊待办", isSel: false,num: str,type: .social)//
            let orderItem = MYGSystemTypeCellModel(image: "order", name: "好友申请", isSel: false,num: order,type: .order)
            let scheduleItem = MYGSystemTypeCellModel(image: "schedule", name: "协会通知", isSel: false,num: time,type: .schedule)
            if isShowSchedule {
                contents = [systemItem, socialItem, orderItem, scheduleItem]
            }else {
                contents = [systemItem, socialItem, orderItem]
            }
            contents[0].isSel = true
            self.collectionView.reloadData()
            
           
        } failed: { _, error in
            MMYGHUD.showToast(text: error)
        }
        
    }
    
    
}
